package com.huang.springcloud.controller;

import com.huang.springcloud.pojo.Dept;
import com.huang.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DeptController {

    @Autowired
    DeptService deptService;

    @Autowired
    DiscoveryClient client;

    @PostMapping("/dept/add")
    public int addDept(@RequestBody Dept dept) {
        return deptService.addDept(dept);
    }

    @GetMapping("/dept/{id}")
    public Dept getById(@PathVariable("id") int id) {
        return deptService.getById(id);
    }

    @GetMapping("/dept/list")
    public List<Dept> getList() {
        return deptService.getList();
    }

    @GetMapping("/dept/dis")
    public Object discovery(){
        //获取微服务列表清单
        List<String> services = client.getServices();
        System.out.println("discovery=>services:" + services);

        List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER");
        for (ServiceInstance instance : instances) {
            System.out.println("host:" + instance.getHost());
            System.out.println("port:" + instance.getPort());
            System.out.println("uri:" + instance.getUri());
        }

        return this.client;
    }
}
